//
//  ViewController.m
//  aaa
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 Dior_3g. All rights reserved.
//

#import "ViewController.h"
#import <MessageUI/MFMailComposeViewController.h>

@interface ViewController ()<MFMailComposeViewControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 100, 150, 50);
    [button setTitle:@"点击" forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor cyanColor];
}

- (void)click {
    
    Class mainClass = NSClassFromString(@"MFMailComposeViewController");
    if (mainClass) {
        
        if ([mainClass canSendMail]) {
            
            [self displayMail];
            
        }else {
            
            [self showMail];
            
        }
        
    }else {
        
        [self showMail];
        
    }
}
//      打开设备自带的邮箱工具
- (void)showMail {
    
    NSString *mail = @"80770010@qq.com";
    mail = [mail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mail]];
}



- (void)displayMail {
    //      创建邮件视图
    MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
    //      收件人地址
    NSArray *recipient = [NSArray arrayWithObject:@"80770010@qq.com"];
    [mail setToRecipients:recipient];
    //      填写主题
    [mail setSubject:@"意见反馈"];
    //      填写邮件内容
    NSString *message = @"...";
    [mail setMessageBody:message isHTML:YES];
    //      添加一个文件
    UIImage *image = [UIImage imageNamed:@"Untitled.jpg"];
    NSData *data = UIImagePNGRepresentation(image);
    [mail addAttachmentData:data mimeType:@"1111" fileName:@"2222"];
    //      设置代理人
    mail.mailComposeDelegate = self;
    
    
    
    
    [self presentViewController:mail animated:YES completion:^{
        
    }];
    
    
    
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    
    NSString *message;
    switch (result) {
        case MFMailComposeResultCancelled:
            message = @"取消发送";
            NSLog(@"%@", message);
            break;
            
            
        case MFMailComposeResultSaved:
            message = @"保存草稿";
            NSLog(@"%@", message);
            break;
            
            
        case MFMailComposeResultSent:
            message = @"已发送";
            NSLog(@"%@", message);
            break;
            
            
        case MFMailComposeResultFailed:
            message = @"发送失败";
            NSLog(@"%@", message);
            break;

    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}







#pragma mark - 使用系统邮件客户端发送邮件
-(void)launchMailApp
{
    NSMutableString *mailUrl = [[NSMutableString alloc]init];
    //添加收件人
    NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
    [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];
    //添加抄送
    NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
    [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];
    //添加密送
    NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
    [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];
    //添加主题
    [mailUrl appendString:@"&subject=my email"];
    //添加邮件内容
    [mailUrl appendString:@"&body=<b>email</b> body!"];
    NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
   
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
}
                                
#pragma mark - 在应用内发送邮件
- (void)sendMailInApp {
    
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (!mailClass) {
        [self alertWithMessage:@"当前系统版本不支持应用内发送邮件功能，您可以使用mailto方法代替"];
        return;
    }
    if (![mailClass canSendMail]) {
        [self alertWithMessage:@"用户没有设置邮件账户"];
        return;
    }
    [self displayMailPicker];
    
}

                                //调出邮件发送窗口
- (void)displayMailPicker
    {
        MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
        mailPicker.mailComposeDelegate = self;
        
        //设置主题
        [mailPicker setSubject: @"eMail主题"];
        //添加收件人
        NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
        [mailPicker setToRecipients: toRecipients];
        //添加抄送
        NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
        [mailPicker setCcRecipients:ccRecipients];
        //添加密送
        NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
        [mailPicker setBccRecipients:bccRecipients];
        
        // 添加一张图片
        UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];
        NSData *imageData = UIImagePNGRepresentation(addPic);            // png
        //关于mimeType：http://www.iana.org/assignments/media-types/index.html
        [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];
        
        //添加一个pdf附件
        NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];
        NSData *pdf = [NSData dataWithContentsOfFile:file];
        [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];
        
        NSString *emailBody = @"<font color='red'>eMail</font> 正文";
        [mailPicker setMessageBody:emailBody isHTML:YES];     
        [self presentModalViewController: mailPicker animated:YES];     
        [mailPicker release];     
    }


#pragma mark - 实现 MFMailComposeViewControllerDelegate
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
    {
        //关闭邮件发送窗口
        [self dismissModalViewControllerAnimated:YES];
        NSString *msg;
        switch (result) {
            case MFMailComposeResultCancelled:
                msg = @"用户取消编辑邮件";
                break;
            case MFMailComposeResultSaved:
                msg = @"用户成功保存邮件";
                break;
            case MFMailComposeResultSent:
                msg = @"用户点击发送，将邮件放到队列中，还没发送";
                break;
            case MFMailComposeResultFailed:
                msg = @"用户试图保存或者发送邮件失败";
                break;
            default:
                msg = @"";   
                break;     
        }     
        [self alertWithMessage:msg];   
    }








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
